﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainScene;
using MindFusion.Diagramming;

namespace SSShell
{
    public partial class ChangeOrigin_Destination : Form
    {
        public frmMain _form;
        public ClassNode selectedN;
        public List<ClassNode> myList = new List<ClassNode>();
        int typ;
        public ChangeOrigin_Destination(frmMain ff,int i)
        {
            InitializeComponent();
            _form = ff;
            typ = i;
            if (i == 1)
            {
                this.Text = "Изменение начальное вершины";
                label1.Text = "Выберете начальную вершину";
            }
            else
            {
                this.Text = "Изменение конечной вершины";
                label1.Text = "Выберете конечную вершину";
            }
        }
        public ClassNode startNode=null,EndNode=null;
        private void ChangeOrigin_Destination_Load(object sender, EventArgs e)
        {
            comboBoxNode.Items.Clear();
            int ind = 0;
            int selind=0;
            if (typ == 1)
            {
                
                foreach (ClassNode item in _form.SS.LstNodes)
                {
                    if (item.TypeN==TypeNode.Scene)
                    {
                        comboBoxNode.Items.Add("Сцена : "+item.Name);
                        
                        myList.Add(item);
                        if (item.Name == ((ClassNode)_form.currentSelectedLink.Origin.Id).Name)
                        {
                            selind = ind;
                            //comboBoxNode.SelectedItem = "Сцена : "+item.Name;
                            startNode = item;
                            selectedN = item;
                        }
                        ind++;
                    }
                }
                
            }
            else
            {

                foreach (ClassNode item in _form.SS.LstNodes)
                {
                    if (item.TypeN == TypeNode.Scene)
                    {
                        comboBoxNode.Items.Add( "Сцена : "+item.Name);
                        if (item.Name == ((ClassNode)_form.currentSelectedLink.Destination.Id).Name)
                        {
                            //comboBoxNode.SelectedItem =  "Сцена : "+item.Name;
                            selind = ind;
                            selectedN = item;
                            EndNode = item;
                        }
                        ind++;
                            
                        
                    }
                    else
                    {
                        comboBoxNode.Items.Add( "Действие : "+item.CaseFrame.ToDo.Name);
                        if (((ClassNode)_form.currentSelectedLink.Destination.Id).CaseFrame!=null)
                            if (item.CaseFrame.ToDo.Name == ((ClassNode)_form.currentSelectedLink.Destination.Id).CaseFrame.ToDo.Name)
                            {
                            
                                //comboBoxNode.SelectedItem = "Действие : "+ item.CaseFrame.ToDo.Name;
                                selind = ind;
                                selectedN = item;
                                EndNode = item;
                            }
                        ind++;
                            
                    }
                    myList.Add(item);
                }
            }
            comboBoxNode.SelectedIndex = selind;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (typ == 1)
            {
                int y = 0;

                foreach (DiagramNode item in _form.diagram1.Nodes)
                {
                    if (y != i && ((ClassNode)item.Id).TypeN==TypeNode.Scene)
                        y++;
                    else
                    {
                        if (((ClassNode)item.Id).TypeN != TypeNode.Action)
                        {
                            startNode.LstNodes.Remove((ClassNode)_form.currentSelectedLink.Destination.Id);
                            int t = 1;
                            foreach (ClassNode item1 in startNode.LstNodes)
                            {
                                foreach (DiagramLink ll in _form.diagram1.Links)
                                {
                                    if (ll.Origin.Id == _form.currentSelectedLink.Origin.Id && (ClassNode)ll.Destination.Id == item1)
                                        ll.Text = "#" + t.ToString();
                                }
                                t++;
                            }
                            _form.currentSelectedLink.Origin = item;
                            ((DiagramLink)_form.currentSelectedLink.Id).Origin = _form.GetShapeNodeById((ClassNode)item.Id);
                            _form.SS.LstNodes[i].LstNodes.Add((ClassNode)_form.currentSelectedLink.Destination.Id);
                            _form.currentSelectedLink.Text = "#" + _form.SS.LstNodes[i].LstNodes.Count.ToString();
                            ((DiagramLink)_form.currentSelectedLink.Id).Text = "#" + _form.SS.LstNodes[i].LstNodes.Count.ToString();
                            break;
                        }
                    }
                }
                _form.currentSelectedLink.Origin.Id = selectedN;
                ((DiagramLink)_form.currentSelectedLink.Id).Origin.Id = selectedN;
            }
            else
            {
                int y = 0;

                foreach (DiagramNode item in _form.diagram1.Nodes)
                {
                    if (y != i)
                        y++;
                    else
                    {
                        EndNode.LstParent.Remove((ClassNode)_form.currentSelectedLink.Origin.Id);
                        int t = 1;
                        foreach (ClassNode item1 in ((ClassNode)_form.currentSelectedLink.Origin.Id).LstNodes)
                        {
                            foreach (DiagramLink ll in _form.diagram1.Links)
                            {
                                if (ll.Origin.Id == _form.currentSelectedLink.Origin.Id && (ClassNode)ll.Destination.Id == item1)
                                    ll.Text = "#" + t.ToString();
                                
                            }
                            t++;
                        }
                        _form.currentSelectedLink.Destination = item;
                        ((DiagramLink)_form.currentSelectedLink.Id).Destination = _form.GetShapeNodeById((ClassNode)item.Id);
                        _form.SS.LstNodes[i].LstParent.Add((ClassNode)_form.currentSelectedLink.Origin.Id);
                        _form.currentSelectedLink.Text = "#" + _form.SS.LstNodes[i].LstParent.Count.ToString();
                        ((DiagramLink)_form.currentSelectedLink.Id).Text = "#" + _form.SS.LstNodes[i].LstParent.Count.ToString();
                        break;
                    }
                }
                _form.currentSelectedLink.Destination.Id = selectedN;
                ((DiagramLink)_form.currentSelectedLink.Id).Destination.Id = selectedN;
            }
        }
        int i = 0;
        private void comboBoxNode_SelectedIndexChanged(object sender, EventArgs e)
        {
             i = comboBoxNode.SelectedIndex;
            selectedN = myList[i];
        }
    }
}
